package com.sailing.xjpb.web.base.advice;

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

/**
 * 处理分页是从1开始情况
 *
 * @author YaoWei
 * createDate: 2022-12-29
 */
public class SailingPageRequest extends PageRequest {
    /**
     * Creates a new {@link SailingPageRequest} with sort parameters applied.
     *
     * @param page zero-based page index, must not be negative.
     * @param size the size of the page to be returned, must be greater than 0.
     * @param sort must not be {@literal null}, use {@link Sort#unsorted()} instead.
     */
    protected SailingPageRequest(int page, int size, Sort sort) {
        super(page, size, sort);
    }

    @Override
    public int getPageNumber() {
        return super.getPageNumber() + 1;
    }

    public static SailingPageRequest of(Pageable pageable) {
        int pageSize = pageable.getPageSize();
        int pageNumber = pageable.getPageNumber();
        Sort sort = pageable.getSort();
        return new SailingPageRequest(pageNumber, pageSize, sort);
    }

}
